package com.sceneworker.monitorpopup.panels;

import java.awt.Dimension;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.concurrent.Callable;

import javax.swing.DefaultListModel;
import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JList;
import javax.swing.JPanel;
import javax.swing.JScrollPane;

import net.miginfocom.swing.MigLayout;

import com.jme.scene.Node;
import com.jme.scene.Spatial;
import com.jme.util.GameTaskQueueManager;
import com.myjavautils.swing.MigLayoutUtils;
import com.myjavautils.swing.SimpleAlert;
import com.myjavautils.swing.SwingComponentFactory;

public class ReorganiseChildrenPanel extends JPanel {
	private		Node		mcl_node;
	private		JList		mcl_children;
	
	public	ReorganiseChildrenPanel(Node cl_node, final JDialog cl_dlg) {
		mcl_node = cl_node;
		
		setLayout(new MigLayout("inset 5"));
		
		intialiseChildrenList();
		
		JButton		lcl_done = SwingComponentFactory.makeAButton("Done");
		lcl_done.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				cl_dlg.setVisible(false);
			}
		});
		
		JButton		lcl_up = SwingComponentFactory.makeAButton("Up");
		lcl_up.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				if(mcl_children.getSelectedIndex() == 0) {
					SimpleAlert.doAlert("Cannot move this child up. It is at the top of the list.", null);
					return;
				}
				
				if(mcl_children.getModel().getSize() == 1) {
					SimpleAlert.doAlert("Cannot move this child as it is the only child of this node.", null);
					return;
				}
				
				GameTaskQueueManager.getManager().update(new Callable<Void>() {
					public Void call() throws Exception {
						int		li_sel = mcl_children.getSelectedIndex();
						mcl_node.swapChildren(li_sel, li_sel - 1);
						mcl_children.setModel(setUpChildrenModel());
						mcl_children.setSelectedIndex(li_sel - 1);
						return null;
					}
				});
			}
		});
		
		JButton		lcl_down = SwingComponentFactory.makeAButton("Down");
		lcl_down.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				if(mcl_children.getSelectedIndex() == (mcl_node.getChildren().size() - 1)) {
					SimpleAlert.doAlert("Cannot move this child down. It is at the bottom of the list.", null);
					return;
				}
				
				if(mcl_children.getModel().getSize() == 1) {
					SimpleAlert.doAlert("Cannot move this child as it is the only child of this node.", null);
					return;
				}
				
				GameTaskQueueManager.getManager().update(new Callable<Void>() {
					public Void call() throws Exception {
						int		li_sel = mcl_children.getSelectedIndex();
						mcl_node.swapChildren(li_sel, li_sel + 1);
						mcl_children.setModel(setUpChildrenModel());
						mcl_children.setSelectedIndex(li_sel + 1);
						return null;
					}
				});
			}
		});
		
		
		MigLayoutUtils.makeRightAlignedButtonBar(this, lcl_done, lcl_up, lcl_down);
	}
	
	private	void	intialiseChildrenList() {
		mcl_children = SwingComponentFactory.makeAList();
		mcl_children.setModel(setUpChildrenModel());
		JScrollPane		lcl_scroll = new JScrollPane();
		lcl_scroll.setViewportView(mcl_children);
		lcl_scroll.setMinimumSize(new Dimension(300, 300));
		add(lcl_scroll, "width 100%, height 100%, wrap");
		
	}
	
	private	DefaultListModel	setUpChildrenModel() {
		DefaultListModel		lcl_model = new DefaultListModel();
		
		for(Spatial lcl_s : mcl_node.getChildren()) {
			lcl_model.addElement(lcl_s);
		}
		
		return lcl_model;
	}
}
